﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SYFramework
{
    public static class GameEventSystem
    {
        
        public static Dictionary<GameEventType, ISubject> GameEventSystems=new Dictionary<GameEventType, ISubject>();

        public static void  RegisterEvent(GameEventType type,IObserver observer)
        {
            ISubject subject = GetSubject(type);
            if (subject!=null)
            {
                subject.AddObserver(observer);
                observer.SetSubject(subject);
            }
        }

        /// <summary>
        /// 注册事件
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private static ISubject GetSubject(GameEventType type)
        {
            if (GameEventSystems.ContainsKey(type))
                return GameEventSystems[type];
            ISubject subject;
            switch (type)
            {
                case GameEventType.UIEvent:
                    subject = new UISubject();
                    break;
                case GameEventType.EnemyEvent:
                    subject=new EnemySubject();
                    break;
                default:
                    Debug.LogError("没有当前枚举的对应的subject" + type);
                    return null;
            }

            GameEventSystems.Add(type, subject);
            return subject;
        }

        /// <summary>
        /// 发送事件
        /// </summary>
        /// <param name="type"></param>
        /// <param name="baseEvent"></param>
        public static void SendSubject(GameEventType type,EventBase baseEvent)
        {
            if (!GameEventSystems.ContainsKey(type))
                return;

            switch (type)
            {
                case GameEventType.UIEvent:
                    GameEventSystems[type].Notfice(baseEvent);
                    break;
                case GameEventType.EnemyEvent:
                    GameEventSystems[type].Notfice(baseEvent);
                    break;
                default:
                    Debug.LogError("没有找到对应类型："+type);
                    return;
            }

        }

        public static void Release()
        {
            GameEventSystems.Clear();
        }
    }

}
